package at.fhooe.stefanirndorfer.entity;

import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2d;
import static org.lwjgl.opengl.GL11.glVertex2i;

import java.util.Random;

import org.lwjgl.input.Keyboard;
import org.newdawn.slick.util.Log;

import at.fhooe.stefanirndorfer.engine.game.Game;
import at.fhooe.stefanirndorfer.sound.SoundManager;
import at.fhooe.stefanirndorfer.sound.SimpleSoundSoundPlayer;
import at.stefanirndorfer.openaltest.entity.Entity;
import at.stefanirndorfer.openaltest.input.InputManager;
import at.stefanirndorfer.openaltest.input.KeyInputListener;




public class MovedRectangle extends Entity implements KeyInputListener{
	
	private static final int STEPWIDTH = 10;
	
	float [] color;
	
	int windowWidth;
	int windowHeight;
	
	//sound stuff
	private SoundManager sm;
	private String[] soundIds;
	SimpleSoundSoundPlayer soundPlayer;
	InputManager im;
	
	boolean leftToRight;
	
	public MovedRectangle(int width, int height, int posX, int posY){
		super(width, height, posX, posY);
		im = Game.getInstance().getInputManager();
		
		this.color = new float[3];
		randomizeColor();
		
		//in order to process input
		setUpKeyInputListener();
		
		windowWidth = Game.getInstance().getWindow().getWindowWidth();
		windowHeight = Game.getInstance().getWindow().getWindwoHeight();
		
		sm = Game.getInstance().getSoundManager();
		initSoundIds();
		
		soundPlayer = new SimpleSoundSoundPlayer(sm, soundIds, windowWidth, windowHeight, posX, posY);
		//sm.addSource(soundPlayer);
		leftToRight = true;
	}
	
	private void randomizeColor() {
		Random rand = new Random();
		this.color[0] = rand.nextFloat();
		this.color[1] = rand.nextFloat();
		this.color[2] = rand.nextFloat();
	}
	
	/**
	 * sounds without ".wav" 
	 */
	private void initSoundIds() {
		this.soundIds = new String[]{
				"Repair",
				"tractor_beam",
				"Footsteps"
		};
	}

	@Override
	public void setUpKeyInputListener() {
		Game.getInstance().getInputManager().addKeyListener(this);				
	}

	@Override
	public void tearDownKeyInputListener(KeyInputListener il) {
		Game.getInstance().getInputManager().removeKeyListener(this);		
	}

	@Override
	public void processKeyInput(int eventKey, boolean keyDown) {
		if(keyDown){
			//fadeout
			if(eventKey == Keyboard.KEY_3 && keyDown){
				soundPlayer.play("tractor_beam", true);
			}
			if(eventKey == Keyboard.KEY_4 && keyDown){
				soundPlayer.stop("tractor_beam");
			}
			if(eventKey == Keyboard.KEY_5 && keyDown){
				soundPlayer.play("Footsteps", true);
			}
			if(eventKey == Keyboard.KEY_6 && keyDown){
				soundPlayer.stop("Footsteps");
			}
			
		}
	}

	@Override
	public void update(int delta) {
		float oldX = (float) posX;
		float oldY = (float) posY;
		
		if(leftToRight){
			if(posX + STEPWIDTH > windowWidth/2-width){
				leftToRight = !leftToRight;
			}else{
				posX += STEPWIDTH;
			}
		}else{
			if(posX - STEPWIDTH < -windowWidth/2){
				leftToRight = !leftToRight;
			}else{
				posX -= STEPWIDTH;
			}
		}
		
		float deltaX = (float)(posX) - oldX;		
		float deltaY = (float)(posY) - oldY;
		float[] sourceParams = new float[]{deltaX, deltaY};
		soundPlayer.updateSourceParams(sourceParams);
	}

	@Override
	public double[] getPosition() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void render() {
		glColor3f(color[0], color[1], color[2]);
		
	

		glBegin(GL_QUADS);
		glVertex2d(posX, posY);
		glVertex2d(posX + width, posY);
		glVertex2d(posX + width, posY + height);
		glVertex2d(posX, posY + height);
		glEnd();
	}

}
